package com.prs.crm.action.cdxh;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.ReportAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.AlreadyDistribution;
import com.prs.crm.domain.cdxh.AlreadyDistributionDtail;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.cdxh.AlreadyDistributionDtailService;
import com.prs.crm.service.cdxh.AlreadyDistributionService;
import com.prs.crm.service.cdxh.DistributionOrderService;

public class DistributionOrderAction extends ReportAction {

	private static final long serialVersionUID = 1L;

	private AlreadyDistribution alreadyDistribution;

	private AlreadyDistributionService alreadyDistributionService;

	private AlreadyDistributionDtailService alreadyDistributionDtailService;

	private Collection<AlreadyDistributionDtail> slist = new ArrayList<AlreadyDistributionDtail>();

	private DistributionOrder distributionOrder;

	@Autowired
	private DistributionOrderService distributionOrderService;

	private Collection<DistributionOrderDetail> productlist;

	private PaginationSupport pagination;

	private Integer[] disIds;

	private File upload;

	private String uploadFileName;;

	private String disTotalMoney;
    
	private String allNus="0";
	
	private String disDates = "2010年12月13";

	private Integer[] checks;

	private String[] packWay;

	private Integer[] proJudges;

	private String[] disNums;

	private String[] disNumss;

	private String[] disTotal;

	private String pagetrunNum;

	private String disxMony = "0";

	private String allids = "";
	
	private List  alistA=new ArrayList();
	
	private List  alistB=new ArrayList();
	
	private String orderCodes="";
	
	private String cusName="";
	
	@Action("editDistribution")
	public String edit() {
		distributionOrder = this.getDistributionOrderService().get(
				distributionOrder.getId());
		productlist = distributionOrder.getDetails();

		disDates = NewTime();

		return SUCCESS;
	}

	/**
	 * 配送
	 * 
	 * @return
	 */
	@Clew(results = { "success" })
	@Action(value = "saveDistribution", results = {
			@Result(name = "success", type = "redirect", location = "listDistributionOrder"),
			@Result(name = "error", type = "chain", location = "editDistribution") })
	public String saveDistribution() {
		if (checks == null) {
			this.setPrompt("该次配送失败,请选择要配送的产品！");
		} else {
			this.getAlreadyDistributionService().setAlreadyDistribution(
					alreadyDistribution, distributionOrder, checks, packWay,
					proJudges, disNums, disNumss, disTotal);
			this.setPrompt("该次配送成功");
		}
		return SUCCESS;
	}

	/**
	 * 配送打印查看
	 * 
	 * @return
	 */
	@Action("printDisbutionsA")
	public String printDisbutionsA() {
		  //时间
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");		
		String sdz=df.format(new Date());
		String dd[]=sdz.split("-");
		disDates=dd[0]+"年"+dd[1]+"月"+dd[2]+"日";
//		System.out.println("pagetrunNum:"+pagetrunNum);
		AlreadyDistributionDtail adObject=this.getAlreadyDistributionDtailService().get(Integer.parseInt(pagetrunNum.split(",")[0]));
		AlreadyDistribution  aobject=this.getAlreadyDistributionService().get(adObject.getAlreadyDistribution().getId());
		alreadyDistribution = this.getAlreadyDistributionService().get(
				aobject.getId());
		//明细计算
       String ad[]=pagetrunNum.split(",");
       double OneM=0.0;//合计金额
       int oneN=0;//合计数量
       for(String  adId:ad){
    	if(!"".equals(adId)){
    		AlreadyDistributionDtail adObjects=this.getAlreadyDistributionDtailService().get(Integer.parseInt(adId));
    		slist.add(adObjects);
    		OneM=OneM+Double.parseDouble(adObjects.getDisTotal());
    		oneN=oneN+Integer.parseInt(adObjects.getProNumber());
    	}
    	
        }
       allNus=oneN+"";

		disTotalMoney = PriceProcess(OneM);
	
		disxMony = OneM + "";
		
		return SUCCESS;
	}

	/**
	 * 配送打印商家选择
	 * 
	 * @return
	 */
	@Action("printDisbutionsB")
	public String printDisbutionsB() {
//		System.out.println("pagetrunNum:" + pagetrunNum);
		  String ad[]=pagetrunNum.split(",");
	      
	       for(String  adId:ad){
	    	if(!"".equals(adId)){
	    		AlreadyDistributionDtail adObjects=this.getAlreadyDistributionDtailService().get(Integer.parseInt(adId));
	    		alistA.add(adObjects.getAlreadyDistribution().getPlanorderCode());
	    		alistB.add(adObjects.getAlreadyDistribution().getCustomerName());
	    	}
	    	
	        }
		
		
	       allids=pagetrunNum;
		
		
		
		return SUCCESS;
	}
	
	
	@Action("printDisbutionsC")
	public String printDisbutionsC() {
//		System.out.println("pagetrunNum:" + pagetrunNum);
//		  String ad[]=pagetrunNum.split(",");
	       //订单编号			
		  //客户名称
			
		  //时间
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");			
		String sdz=df.format(new Date());
		String dd[]=sdz.split("-");
		disDates=dd[0]+"年"+dd[1]+"月"+dd[2]+"日";
			
//	     AlreadyDistributionDtail adObject=this.getAlreadyDistributionDtailService().get(Integer.parseInt(pagetrunNum.split(",")[0]));
//			AlreadyDistribution  aobject=this.getAlreadyDistributionService().get(adObject.getAlreadyDistribution().getId());
//			alreadyDistribution = this.getAlreadyDistributionService().get(
//					aobject.getId());
			//明细计算
	       String adx[]=pagetrunNum.split(",");
	       double OneM=0.0;//合计金额
	       int oneN=0;//合计数量
	       for(String  adId:adx){
	    	if(!"".equals(adId)){
	    		AlreadyDistributionDtail adObjects=this.getAlreadyDistributionDtailService().get(Integer.parseInt(adId));
	    		slist.add(adObjects);
	    		OneM=OneM+Double.parseDouble(adObjects.getDisTotal());
	    		oneN=oneN+Integer.parseInt(adObjects.getProNumber());
	    	}
	    	
	        }
	       allNus=oneN+"";

			disTotalMoney = PriceProcess(OneM);
		
			disxMony = OneM + "";
		
		
		return SUCCESS;
	}
	/**
	 * 多个产品配送打印
	 * 
	 * @return
	 */

	@Clew(results = { "success,cuscheck" })
	@Action(value = "distributionprint", results = {
			@Result(name = "success", type = "redirect", location = "printDisbutionsA?pagetrunNum=${pagetrunNum}"),
			@Result(name = "cuscheck", type = "redirect", location = "printDisbutionsB?pagetrunNum=${pagetrunNum}"),
			@Result(name = "error", type = "chain", location = "listDistributiondetailPass") })
	public String distributionprint() {
		int z = 0;
		String ids[] = pagetrunNum.split(",");
		List<AlreadyDistribution> psObject = new ArrayList<AlreadyDistribution>();
		for (String is : ids) {
			if ("".equals(is)) {
				// 选择了相同客户的产品
			} else {
				// 选择了不同客户的产品
				AlreadyDistributionDtail detailObject = this
						.getAlreadyDistributionDtailService().get(
								Integer.parseInt(is));
				if (psObject.size() == 0) {
					psObject.add(detailObject.getAlreadyDistribution());
				} else {
					for (AlreadyDistribution aaObject : psObject) {
						if (aaObject.equals(detailObject
								.getAlreadyDistribution())) {

						} else {
							z++;
						}
					}
				}

			}
		}
		if (z == 0) {
			return SUCCESS;
		} else {
			return "cuscheck";
		}

	}

	@Action("listDistributionOrder")
	public String listDistributionOrder() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DistributionOrder.class);
		criteria.add(Restrictions.eq("state", "0"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionOrderService().listByPage(
				criteria, pagination));

		return SUCCESS;
	}

	/**
	 * 以配送单据
	 * 
	 * @return
	 */
	@Action("listDistributionPass")
	public String listDistributionPass() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AlreadyDistribution.class);

		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAlreadyDistributionService().listByPage(
				criteria, pagination));

		return SUCCESS;
	}

	/**
	 * 以配送单据 明细
	 * 
	 * @return
	 */
	@Action("listDistributiondetailPass")
	public String listDistributiondetailPass() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AlreadyDistributionDtail.class);

		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAlreadyDistributionDtailService()
				.listByPage(criteria, pagination));

		return SUCCESS;
	}

	@Action(value = "exportDistributionReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String reportOrder() {
		ReportTable table = new ReportTable("配送单");
		table.addCell(new ReportCell("日期", "recordTime", "yyyy-MM-dd"));
		table.addCell(new ReportCell("客户名称", "proName"));
		table.addCell(new ReportCell("颜色", "proColor"));
		table.addCell(new ReportCell("规格", "proNorm"));
		table.addCell(new ReportCell("数量(米)", "proNumber"));
		table.addCell(new ReportCell("单价(元/米)", "proPrice"));
		table.addCell(new ReportCell("金额(元)", "disTotal"));
		table.addCell(new ReportCell("件数", "proPackWay"));
		table.addCell(new ReportCell("送货单号", "discode"));

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AlreadyDistributionDtail.class);
		this.setOutFilePath(this.getReportService().exportAlreadyDistribution(
				criteria, table, new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	@Action("viewAlreadyDistribution")
	public String viewAlreadyDistribution() {

		alreadyDistribution = this.getAlreadyDistributionService().get(
				alreadyDistribution.getId());
		// System.out.println(alreadyDistribution.getTotalMony()+"AAAAA");
		slist = alreadyDistribution.getDetails();

		disTotalMoney = PriceProcess(alreadyDistribution.getTotalMony());
		// System.out.println(disTotalMoney+"aaaaa");
		disxMony = alreadyDistribution.getTotalMony() + "";
		return SUCCESS;
	}

	@Action("viewDistribution")
	public String viewDistribution() {
		distributionOrder = this.getDistributionOrderService().get(
				distributionOrder.getId());
		productlist = distributionOrder.getDetails();

		disDates = NewTime();
		// disTotalMoney=PriceProcess(distributionOrder.getDisTotalMoney());
		return SUCCESS;
	}

	public String NumZ(String Num) {
		String printValue = "零";
		int bvalue = Integer.parseInt(Num);
		if (bvalue == 0) {
			printValue = "零";
		} else if (bvalue == 1) {
			printValue = "壹";
		} else if (bvalue == 2) {
			printValue = "贰";
		} else if (bvalue == 3) {
			printValue = "叁";
		} else if (bvalue == 4) {
			printValue = "肆";
		} else if (bvalue == 5) {
			printValue = "伍";
		} else if (bvalue == 6) {
			printValue = "陆";
		} else if (bvalue == 7) {
			printValue = "柒";
		} else if (bvalue == 8) {
			printValue = "捌";
		} else if (bvalue == 9) {
			printValue = "玖";
		}
		return printValue;
	}

	public String WPrint(String W) {
		String zOver = "";
		for (int i = 0; i < W.length(); i++) {
			zOver = zOver + NumZ(W.charAt(i) + "");
		}

		return zOver.trim();
	}

	public String PriceProcess(double prices) {
		String backValueA = "";
		String backValueB = "";
		String Figures[] = (prices + "").split("\\.");

		if (Figures[0].length() == 1) {
			backValueA = "零万零千零佰零拾" + NumZ(Figures[0]) + "元";
		} else if (Figures[0].length() == 2) {
			backValueA = "零万零千零佰" + NumZ(Figures[0].charAt(0) + "") + "拾"
					+ NumZ(Figures[0].charAt(1) + "") + "元";
		} else if (Figures[0].length() == 3) {
			backValueA = "零万零千" + NumZ(Figures[0].charAt(0) + "") + "佰"
					+ NumZ(Figures[0].charAt(1) + "") + "拾"
					+ NumZ(Figures[0].charAt(2) + "") + "元";
		} else if (Figures[0].length() == 4) {
			backValueA = "零万" + NumZ(Figures[0].charAt(0) + "") + "千"
					+ NumZ(Figures[0].charAt(1) + "") + "佰"
					+ NumZ(Figures[0].charAt(2) + "") + "拾"
					+ NumZ((Figures[0].charAt(3) + "")) + "元";
		} else if (Figures[0].length() == 5) {
			backValueA = NumZ(Figures[0].charAt(0) + "") + "万"
					+ NumZ(Figures[0].charAt(1) + "") + "千"
					+ NumZ(Figures[0].charAt(2) + "") + "佰"
					+ NumZ(Figures[0].charAt(3) + "") + "拾"
					+ NumZ((Figures[0].charAt(4) + "")) + "元";
		} else if (Figures[0].length() > 5) {
			int firstA = Integer.parseInt(Figures[0]);
			int mang = firstA / 10000;
			int yu = firstA % 10000;
			String nn = yu + "";
			backValueA = WPrint(mang + "") + "万" + NumZ(nn.charAt(0) + "")
					+ "千" + NumZ(nn.charAt(1) + "") + "佰"
					+ NumZ(nn.charAt(2) + "") + "拾" + NumZ((nn.charAt(3) + ""))
					+ "元";

		}

		if (Figures[1].length() == 1) {
			backValueB = NumZ(Figures[1].charAt(0) + "") + "角";
		} else if (Figures[1].length() > 1) {
			int hx = Integer.parseInt(Figures[1].charAt(1) + "");
			if (hx >= 5) {
				int hxx = Integer.parseInt(Figures[1].charAt(0) + "") + 1;
				backValueB = NumZ(hxx + "") + "角";
			} else {
				backValueB = NumZ(Figures[1].charAt(0) + "") + "角";
			}
		}

		String AB = backValueA + backValueB;

		return AB;

	}

	public String NewTime() {
		int yyyy, MM, dd;
		Calendar c = Calendar.getInstance();
		yyyy = c.get(Calendar.YEAR);
		MM = c.get(Calendar.MONTH) + 1;
		dd = c.get(Calendar.DAY_OF_MONTH);
		String Times = yyyy + "年" + MM + "月" + dd + "日";
		return Times;
	}

	public DistributionOrder getDistributionOrder() {
		return distributionOrder;
	}

	public void setDistributionOrder(DistributionOrder distributionOrder) {
		this.distributionOrder = distributionOrder;
	}

	public DistributionOrderService getDistributionOrderService() {
		return distributionOrderService;
	}

	public void setDistributionOrderService(
			DistributionOrderService distributionOrderService) {
		this.distributionOrderService = distributionOrderService;
	}

	public Integer[] getDisIds() {
		return disIds;
	}

	public void setDisIds(Integer[] disIds) {
		this.disIds = disIds;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Collection<DistributionOrderDetail> getProductlist() {
		return productlist;
	}

	public void setProductlist(Collection<DistributionOrderDetail> productlist) {
		this.productlist = productlist;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getDisTotalMoney() {
		return disTotalMoney;
	}

	public void setDisTotalMoney(String disTotalMoney) {
		this.disTotalMoney = disTotalMoney;
	}

	public String getDisDates() {
		return disDates;
	}

	public void setDisDates(String disDates) {
		this.disDates = disDates;
	}

	public AlreadyDistribution getAlreadyDistribution() {
		return alreadyDistribution;
	}

	public void setAlreadyDistribution(AlreadyDistribution alreadyDistribution) {
		this.alreadyDistribution = alreadyDistribution;
	}

	public AlreadyDistributionService getAlreadyDistributionService() {
		return alreadyDistributionService;
	}

	public void setAlreadyDistributionService(
			AlreadyDistributionService alreadyDistributionService) {
		this.alreadyDistributionService = alreadyDistributionService;
	}

	public Collection<AlreadyDistributionDtail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<AlreadyDistributionDtail> slist) {
		this.slist = slist;
	}

	public Integer[] getChecks() {
		return checks;
	}

	public void setChecks(Integer[] checks) {
		this.checks = checks;
	}

	public String[] getPackWay() {
		return packWay;
	}

	public void setPackWay(String[] packWay) {
		this.packWay = packWay;
	}

	public Integer[] getProJudges() {
		return proJudges;
	}

	public void setProJudges(Integer[] proJudges) {
		this.proJudges = proJudges;
	}

	public String[] getDisNums() {
		return disNums;
	}

	public void setDisNums(String[] disNums) {
		this.disNums = disNums;
	}

	public String[] getDisNumss() {
		return disNumss;
	}

	public void setDisNumss(String[] disNumss) {
		this.disNumss = disNumss;
	}

	public String[] getDisTotal() {
		return disTotal;
	}

	public void setDisTotal(String[] disTotal) {
		this.disTotal = disTotal;
	}

	public String getDisxMony() {
		return disxMony;
	}

	public void setDisxMony(String disxMony) {
		this.disxMony = disxMony;
	}

	public AlreadyDistributionDtailService getAlreadyDistributionDtailService() {
		return alreadyDistributionDtailService;
	}

	public void setAlreadyDistributionDtailService(
			AlreadyDistributionDtailService alreadyDistributionDtailService) {
		this.alreadyDistributionDtailService = alreadyDistributionDtailService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getAllids() {
		return allids;
	}

	public void setAllids(String allids) {
		this.allids = allids;
	}

	public String getPagetrunNum() {
		return pagetrunNum;
	}

	public void setPagetrunNum(String pagetrunNum) {
		this.pagetrunNum = pagetrunNum;
	}

	public String getAllNus() {
		return allNus;
	}

	public void setAllNus(String allNus) {
		this.allNus = allNus;
	}

	public List getAlistA() {
		return alistA;
	}

	public void setAlistA(List alistA) {
		this.alistA = alistA;
	}

	public List getAlistB() {
		return alistB;
	}

	public void setAlistB(List alistB) {
		this.alistB = alistB;
	}

	public String getOrderCodes() {
		return orderCodes;
	}

	public void setOrderCodes(String orderCodes) {
		this.orderCodes = orderCodes;
	}

	public String getCusName() {
		return cusName;
	}

	public void setCusName(String cusName) {
		this.cusName = cusName;
	}

}
